import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/3/18
 * \* Time: 11:01
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class Delect {
    public static void main(String[] args) throws SQLException {
        //删除数据库中的记录
        //让用户输入一个id，根据id来删除
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();

        //输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id:");
        int id = scanner.nextInt();

        //3.拼装SQL语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        //4.执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);

        //5.释放资源
        statement.close();
        connection.close();

    }
}
